package com.itlong.cloud.iot.huawei.util;

import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.utils.network.https.HttpsPost;
import com.itlong.cloud.utils.node.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;


/**
 * <desc>
 *      华为云iot request client
 * </desc>
 *
 * @createDate 2020/01/06
 */
@Component
@RefreshScope
public class HuawRequestClient {
    @Value("${" + PlatformConstants.HUAWEI_IOT_SERVICE_URL + "}")
    private String url;
    @Value("${" + PlatformConstants.HUAWEI_IOT_SERVICE_PORT + "}")
    private String port;
    @Value("${" + PlatformConstants.HUAWEI_IOT_APPID + "}")
    private String appid;
    @Value("${" + PlatformConstants.HUAWEI_IOT_SECRET + "}")
    private String secret;
    @Value("${" + PlatformConstants.HUAWEI_PROJECT_ID + "}")
    private String projectId;
    @Value("${" + PlatformConstants.HUAWEI_IOT_STATE_URL + "}")
    private String stateUrl;
    @Value("${" + PlatformConstants.HUAWEI_IOT_INFO_URL + "}")
    private String infoUrl;

    @Autowired
    HuawTokenClient huawTokenClient;



    /**
     * <desc>
     *      北向接口实例获取
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2020/01/06
     */
    @PostConstruct
    public  void initApiClient() throws Exception {
//        while(true) {
//            //订阅设备变化数据
//            Map<String, Object> params = new HashMap<>();
//            params.put("callbackurl", infoUrl);
//            params.put("channel", "http");
//            Map<String, Object> subject = new HashMap<>();
//            subject.put("resource", "device.data");
//            subject.put("event", "update");
//            params.put("subject", subject);
//            HttpsPost.https(huawTokenClient.getAccessToken(), "https://iotdm.cn-north-4.myhuaweicloud.com/v5/iot/" + projectId + "/subscriptions", JsonUtil.toJSON(params));
//        }
    }

    public static void main(String[] args) throws Exception {
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> auth = new HashMap<>();
        Map<String,Object> identity = new HashMap<>();
        Map<String,Object> password = new HashMap<>();
        Map<String,Object> user = new HashMap<>();
        Map<String,Object> domain = new HashMap<>();
        Map<String,Object> scope = new HashMap<>();
        Map<String,Object> project = new HashMap<>();
        String[] method = new String[]{"password"};
        user.put("name","hw29477178");
        user.put("password","Dy86675901");
        user.put("domain",domain);
        domain.put("name","hw29477178");
        project.put("name","cn-north-4");
        scope.put("project",project);
        password.put("user",user);
        identity.put("methods",method);
        identity.put("password",password);
        auth.put("identity",identity);
        auth.put("scope",scope);
        params.put("auth",auth);
        String token = HttpsPost.httpsGetToken("https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens", params);

        String result = HttpsPost.httpsGet(token,"https://iotdm.cn-north-4.myhuaweicloud.com/v5/iot/075e638b380025032f5dc01cc8403111/subscriptions",null);
        //订阅设备变化数据
        Map<String, Object> b = new HashMap<>();
        b.put("callbackurl", "www.b.com/123");
        b.put("channel", "http");
        Map<String, Object> subject = new HashMap<>();
        subject.put("resource", "device.properties");
        subject.put("event", "update");
        b.put("subject", subject);
        String a = HttpsPost.https(token,"https://iotdm.cn-north-4.myhuaweicloud.com/v5/iot/075e638b380025032f5dc01cc8403111/subscriptions", JsonUtil.toJSON(b));
    }
}